//
//  AllMusicVC.m
//  K-Music
//
//  Created by 林杰良 on 16/1/1.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "AllMusicVC.h"
#import "MyAllText.h"
#import "UIImageView+WebCache.h"
#import "SameWay.h"
#import "A_Music.h"


@interface AllMusicVC ()<UIScrollViewDelegate>{
    UIScrollView*myHscrollview;
    
    UIButton * selectedBtn;
    
    UIView*redView;

    UIView *firstView;
    
    UIView*secondView;
    
    NSMutableArray*aa;
    NSMutableArray*bb;
    NSMutableArray*cc;
    NSMutableArray*dd;

    
}
@property (strong,nonatomic)UIButton * tmpBtn;

@end

@implementation AllMusicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    [self creatUI];
    
    self.navigationItem.title = @"总榜";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
}

- (void)play_play
{
    NSLog(@"play");
    
    //[MBProgressHUD showErrorWithStatus:@"播放器" toView:self.view];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"music_play" object:self];
}

- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark  创建ui
-(void)creatUI{
    NSArray*textData;
       textData=[NSArray arrayWithObjects:@"作品榜",@"歌手榜",@"财富榜",nil];
    myHscrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [myHscrollview setContentSize:CGSizeMake(WIDTH/3, 40)];
    [myHscrollview setShowsHorizontalScrollIndicator:NO];
    [myHscrollview setShowsVerticalScrollIndicator:NO];
    [myHscrollview setDelegate:self];
    myHscrollview.backgroundColor=[UIColor clearColor];
    [self.view addSubview:myHscrollview];

    _tmpBtn=nil;
    
    for (int i=0; i<textData.count; i++) {
        UIButton*topBtn=[[UIButton alloc]initWithFrame:CGRectMake(i*WIDTH/3, 1,WIDTH/3-1, 40-2)];
        topBtn.backgroundColor=[UIColor whiteColor];
        [topBtn setTitle:[NSString stringWithFormat:@"%@",textData[i]] forState:UIControlStateNormal];
        [topBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [topBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        topBtn.userInteractionEnabled = YES;
        topBtn.tag=i+100;
        [topBtn addTarget:self action:@selector(TopButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        if ([[MyAllText useIt].clickIt_number intValue]==i) {
            topBtn.selected=YES;
            selectedBtn=topBtn;
            
        }
        
        [myHscrollview addSubview:topBtn];
        
    }
    
    redView=[[UIView alloc]initWithFrame:CGRectMake(0, 105, WIDTH, HEIGHT-100)];
    redView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:redView];
    
    [self gainMuiscData:@"B"];

    
}
#pragma mark--点击事件请求数据
-(void)TopButtonClick:(UIButton*)btn{
    
    NSString*number=[NSString stringWithFormat:@"%ld",(long)btn.tag];
    int clickNumber=[number intValue]-100;
    NSString*cn=[NSString stringWithFormat:@"%d",clickNumber];
    [MyAllText useIt].clickIt_number=cn;
    selectedBtn.selected = NO;
    btn.selected = YES;
    selectedBtn = btn;
    
    if (btn.tag==100) {
        [secondView removeFromSuperview];
        [firstView removeFromSuperview];
        [self gainMuiscData:@"B"];
        
    }else if(btn.tag==101){
        [secondView removeFromSuperview];
        [firstView removeFromSuperview];
        
        [self downloadData:@"B"];
        
    }

}

-(void)gainMuiscData:(NSString*)valve{
    
    [SameWay downloadData:@"B" andCallback:^(id obj) {
        NSLog(@"===========:%@",obj);
        
        cc=[NSMutableArray array];
        
        cc=obj;
        NSLog(@"%@",cc);
        [self creatTheMusic];
        
    }];
}

-(void)downloadData:(NSString*)value{
    [SameWay downloadData:@"B" andCallback:^(id obj) {
        NSLog(@"===========:%@",obj);
        
        aa=[NSMutableArray array];
        
        aa=obj;
        NSLog(@"%@",aa);
        [self creatTheTopView];

    }];
}

#pragma mark---作品榜
-(void)creatTheMusic{
    

    firstView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100)];
    firstView.backgroundColor=[UIColor whiteColor];
    [redView addSubview:firstView];
    
    UIScrollView*myscrollview=[[UIScrollView alloc ]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100)];
    myscrollview.delegate=self;
    myscrollview.contentSize=CGSizeMake(WIDTH, HEIGHT+30);

    [firstView addSubview:myscrollview];
    
    
    UIView*aaView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2)];
    aaView.backgroundColor=[UIColor whiteColor];
    [myscrollview addSubview:aaView];
    
    UILabel *top=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    top.text=@"    |最火作品总榜";
    top.font=[UIFont systemFontOfSize:13.0f];
    [aaView addSubview:top];
    
    
    UIView*h_view_1=[[UIView alloc]initWithFrame:CGRectMake(0, top.frame.size.height+top.frame.origin.y, WIDTH, HEIGHT/2)];
    h_view_1.backgroundColor=[UIColor whiteColor];
    [aaView addSubview: h_view_1];
    
    A_Music*aaa=[[A_Music alloc]init];
    for (int j=0; j<3; j++) {
        aaa=cc[j];
        if (j==0) {
            UIView*h_view=[[UIView alloc]initWithFrame:CGRectMake(0, 1, WIDTH, HEIGHT/4-5)];
            h_view.backgroundColor=[UIColor whiteColor];
            [h_view_1 addSubview: h_view];
            
            UIImageView*dt=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  WIDTH, HEIGHT/4-5)];
            [dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",aaa.pictureUrl]] placeholderImage:nil];

            [h_view addSubview:dt];
            
            UIImageView *top_img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
            top_img.image=[UIImage imageNamed:@"1."];
            [dt addSubview:top_img];
            
            UILabel*mylabel=[[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT/8,  WIDTH/2, 30)];
            mylabel.text=[NSString stringWithFormat:@"%@",aaa.nickName];
            mylabel.textColor=[UIColor whiteColor];
            
            mylabel.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: mylabel];
            
            UILabel*my_name=[[UILabel alloc]initWithFrame:CGRectMake(5, dt.frame.size.height-30, WIDTH/6, 30)];
            my_name.text=[NSString stringWithFormat:@"%@",aaa.name];
            my_name.textColor=[UIColor whiteColor];
            my_name.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_name];
            
            UILabel*my_fun=[[UILabel alloc]initWithFrame:CGRectMake(my_name.frame.size.width+my_name.frame.origin.x +10, dt.frame.size.height-30, WIDTH/6, 30)];
            my_fun.text=[NSString stringWithFormat:@"%@",aaa.totalShare];
            my_fun.textColor=[UIColor whiteColor];
            
            my_fun.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_fun];
            
        }else{
            
            UIView*h_view=[[UIView alloc]initWithFrame:CGRectMake((j-1)*WIDTH/2-1, HEIGHT/4-5, WIDTH/2-1, HEIGHT/4-50)];
            h_view.backgroundColor=[UIColor whiteColor];
            [h_view_1 addSubview: h_view];
            
            UIImageView*dt=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  WIDTH/2, HEIGHT/4-5)];
//            dt.image=[UIImage imageNamed:@"组-3"];
            [dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",aaa.pictureUrl]] placeholderImage:nil];

            [h_view addSubview:dt];
            
            UIImageView *top_img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
            top_img.image=[UIImage imageNamed:@"1."];
            [dt addSubview:top_img];
            
            UILabel*mylabel=[[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT/8,  WIDTH/2, 30)];
            mylabel.text=[NSString stringWithFormat:@"%@",aaa.nickName];
            mylabel.textColor=[UIColor whiteColor];
            
            mylabel.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: mylabel];
            
            UILabel*my_name=[[UILabel alloc]initWithFrame:CGRectMake(5, dt.frame.size.height-40, WIDTH/6, 30)];
            my_name.text=[NSString stringWithFormat:@"%@",aaa.name];
            my_name.textColor=[UIColor whiteColor];
            my_name.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_name];
            
            UILabel*my_fun=[[UILabel alloc]initWithFrame:CGRectMake(my_name.frame.size.width+my_name.frame.origin.x +10, dt.frame.size.height-40, WIDTH/6, 30)];
            my_fun.text=[NSString stringWithFormat:@"%@",aaa.totalShare];
            my_fun.textColor=[UIColor whiteColor];
            
            my_fun.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_fun];
        }
    }
    
    UIView*bbView=[[UIView alloc]initWithFrame:CGRectMake(0, aaView.frame.origin.y+aaView.frame.size.height+15, WIDTH, HEIGHT/2)];
    bbView.backgroundColor=[UIColor whiteColor];
    [myscrollview addSubview:bbView];
    
    UILabel *hot=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    hot.text=@"   |最火作品总榜";
    hot.font=[UIFont systemFontOfSize:13.0f];
    [bbView addSubview:hot];
    
    UIView*hot_view_1=[[UIView alloc]initWithFrame:CGRectMake(0, top.frame.size.height+top.frame.origin.y, WIDTH, HEIGHT/2)];
    hot_view_1.backgroundColor=[UIColor whiteColor];
    [bbView addSubview: hot_view_1];
    
    for (int i=0; i<3; i++) {
        aaa=cc[i];
        if (i==0) {
            UIView*h_view=[[UIView alloc]initWithFrame:CGRectMake(0, 1, WIDTH, HEIGHT/4-5)];
            h_view.backgroundColor=[UIColor whiteColor];
            [hot_view_1 addSubview: h_view];
            
            UIImageView*dt=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  WIDTH, HEIGHT/4-5)];
            [dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",aaa.pictureUrl]] placeholderImage:nil];
            
            [h_view addSubview:dt];
            
            UIImageView *top_img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
            top_img.image=[UIImage imageNamed:@"1."];
            [dt addSubview:top_img];
            
            UILabel*mylabel=[[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT/8,  WIDTH/2, 30)];
            mylabel.text=[NSString stringWithFormat:@"%@",aaa.nickName];
            mylabel.textColor=[UIColor whiteColor];
            
            mylabel.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: mylabel];
            
            
            UILabel*my_name=[[UILabel alloc]initWithFrame:CGRectMake(5, dt.frame.size.height-30, WIDTH/6, 30)];
            my_name.text=[NSString stringWithFormat:@"%@",aaa.name];
            my_name.textColor=[UIColor whiteColor];
            my_name.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_name];
            
            UILabel*my_fun=[[UILabel alloc]initWithFrame:CGRectMake(my_name.frame.size.width+my_name.frame.origin.x +10, dt.frame.size.height-30, WIDTH/6, 30)];
            my_fun.text=[NSString stringWithFormat:@"%@",aaa.totalShare];
            my_fun.textColor=[UIColor whiteColor];
            
            my_fun.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_fun];
        }else{
            UIView*h_view=[[UIView alloc]initWithFrame:CGRectMake((i-1)*WIDTH/2-1, HEIGHT/4-5, WIDTH/2-1, HEIGHT/4-50)];
            h_view.backgroundColor=[UIColor whiteColor];
            [hot_view_1 addSubview: h_view];
            
            UIImageView*dt=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  WIDTH/2, HEIGHT/4-5)];
            //            dt.image=[UIImage imageNamed:@"组-3"];
            [dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",aaa.pictureUrl]] placeholderImage:nil];
            
            [h_view addSubview:dt];
            
            UIImageView *top_img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
            top_img.image=[UIImage imageNamed:@"1."];
            [dt addSubview:top_img];
            
            UILabel*mylabel=[[UILabel alloc]initWithFrame:CGRectMake(0, HEIGHT/8,  WIDTH/2, 30)];
            mylabel.text=[NSString stringWithFormat:@"%@",aaa.nickName];
            mylabel.textColor=[UIColor whiteColor];
            
            mylabel.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: mylabel];
            
            
            UILabel*my_name=[[UILabel alloc]initWithFrame:CGRectMake(5, dt.frame.size.height-40, WIDTH/6, 30)];
            my_name.text=[NSString stringWithFormat:@"%@",aaa.name];
            my_name.textColor=[UIColor whiteColor];
            my_name.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_name];
            
            UILabel*my_fun=[[UILabel alloc]initWithFrame:CGRectMake(my_name.frame.size.width+my_name.frame.origin.x +10, dt.frame.size.height-40, WIDTH/6, 30)];
            my_fun.text=[NSString stringWithFormat:@"%@",aaa.totalShare];
            my_fun.textColor=[UIColor whiteColor];
            
            my_fun.textAlignment=NSTextAlignmentCenter;
            [dt addSubview: my_fun];
        }
    }

}
#pragma mark  --第二部分财富榜与歌手帮
-(void)creatTheTopView{
    
    secondView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100)];
    secondView.backgroundColor=[UIColor clearColor];
    [redView addSubview:secondView];
    
    UIScrollView*myscrollview=[[UIScrollView alloc ]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-100)];
    myscrollview.delegate=self;
    myscrollview.contentSize=CGSizeMake(WIDTH, HEIGHT+60);
    
    [secondView addSubview:myscrollview];
    
    UILabel *top=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, WIDTH, 30)];
    top.text=@"财富总榜";
    top.textColor=[UIColor orangeColor];
    top.font=[UIFont systemFontOfSize:13.0f];
    [myscrollview addSubview:top];
    
    
    UIView*mk=[[UIView alloc]initWithFrame:CGRectMake(0,top.frame.size.height+top.frame.origin.y, WIDTH, HEIGHT/2)];
    mk.backgroundColor=[UIColor whiteColor];
    [myscrollview addSubview:mk];

    A_Music*music=[[A_Music alloc]init];

    for (int i=0; i<4; i++) {
        
        music=aa[i];

        UIView*mkey=[[UIView alloc]initWithFrame:CGRectMake(0, i*HEIGHT/8, WIDTH, HEIGHT/8)];
        mkey.layer.borderWidth = 0.5;
        mkey.layer.borderColor = [[UIColor blackColor] CGColor];
        [mk addSubview:mkey];
        
        
        UILabel*number=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 20, HEIGHT/8-10)];
        number.text=[NSString stringWithFormat:@"%d",i];
        [mkey addSubview:number];
        
        UIImageView*Teximage=[[UIImageView alloc]initWithFrame:CGRectMake(number.frame.size.width+10  , 5, 60 , 60)];
        Teximage.layer.masksToBounds = YES;
        Teximage.layer.cornerRadius=30.0f;
        [Teximage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",music.pictureUrl]] placeholderImage:nil];

        [mkey addSubview:Teximage];
        
        
        UILabel *Texname=[[UILabel alloc]initWithFrame:CGRectMake(Teximage.frame.size.width+Teximage.frame.origin.x+20, 10, WIDTH/3, 30)];
        Texname.text=[NSString stringWithFormat:@"%@",music.name];
        Texname.textColor=[UIColor orangeColor];
        [mkey addSubview:Texname];
        
        UILabel*Texinfo=[[UILabel alloc]initWithFrame:CGRectMake(Teximage.frame.size.width+Teximage.frame.origin.x+20, 40, WIDTH/3, 30)];
        Texinfo.text=[NSString stringWithFormat:@"%@",music.nickName];
        [mkey addSubview:Texinfo];
        
        
        
    }
    
 //------part_two--
    UILabel *moontop=[[UILabel alloc]initWithFrame:CGRectMake(10, top.frame.size.height+top.frame.origin.y+HEIGHT/2, WIDTH, 30)];
    moontop.text=@"财富总榜";
    moontop.textColor=[UIColor orangeColor];
    moontop.font=[UIFont systemFontOfSize:13.0f];
    [myscrollview addSubview:moontop];
    
    
    UIView*moon=[[UIView alloc]initWithFrame:CGRectMake(0,moontop.frame.size.height+moontop.frame.origin.y, WIDTH, HEIGHT/2)];
    moon.backgroundColor=[UIColor whiteColor];
    [myscrollview addSubview:moon];
    
    for (int i=0; i<4; i++) {
        music=aa[i];

        UIView*mkey=[[UIView alloc]initWithFrame:CGRectMake(0, i*HEIGHT/8, WIDTH, HEIGHT/8)];
        mkey.layer.borderWidth = 0.5;
        mkey.layer.borderColor = [[UIColor blackColor] CGColor];
        [moon addSubview:mkey];
        
        
        UILabel*number=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 20, HEIGHT/8-10)];
        number.text=[NSString stringWithFormat:@"%d",i];
        [mkey addSubview:number];
        
        UIImageView*Teximage=[[UIImageView alloc]initWithFrame:CGRectMake(number.frame.size.width+10  , 5, 60 , 60)];
        Teximage.layer.masksToBounds = YES;
        Teximage.layer.cornerRadius=30.0f;
        [Teximage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",music.pictureUrl]] placeholderImage:nil];
        [mkey addSubview:Teximage];
        
        
        UILabel *Texname=[[UILabel alloc]initWithFrame:CGRectMake(Teximage.frame.size.width+Teximage.frame.origin.x+20, 10, WIDTH/3, 30)];
        Texname.text=[NSString stringWithFormat:@"%@",music.name];
        Texname.textColor=[UIColor orangeColor];
        [mkey addSubview:Texname];
        
        UILabel*Texinfo=[[UILabel alloc]initWithFrame:CGRectMake(Teximage.frame.size.width+Teximage.frame.origin.x+20, 40, WIDTH/3, 30)];
        Texinfo.text=[NSString stringWithFormat:@"%@",music.nickName];
        [mkey addSubview:Texinfo];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
